/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DBFunction;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import Storage.DateManipulation;

public class fNewMessages {
	public static String process(String msgRequest)
			throws JSONException {
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		JSONObject obj = new JSONObject();
		obj = (JSONObject) new JSONTokener(msgRequest).nextValue();
		String currentPhoneNumber = obj.getString("currentPhoneNumber");
		String lastmsgid = obj.getString("lastmsgid");		
		String sql = "SELECT message.UserId,FirstName, LastName,message.MessageId, message.Content, message.Time from buddytracking.user, buddytracking.message where GroupId = ( select GroupId from buddytracking.user where User.PhoneNumber='"+currentPhoneNumber+"') and message.UserId = user.UserId and MessageId >"+lastmsgid;
		ResultSet resultSet = DB.connectDB.ExecuteQuery(sql);

		try {
			while (resultSet.next()) {			
					String sendername = resultSet.getString("FirstName")+" "+ resultSet.getString("LastName");
					object.put("msgid",resultSet.getString("MessageId"));
					object.put("sendername", sendername);					
					object.put("msgcontent", resultSet.getString("Content"));
					object.put("time", resultSet.getString("Time"));		
					array.put(object.toString());				
			}
		} catch (SQLException ex) {
		}
		return array.toString();
	}
}
